package com.example.e

import android.app.ProgressDialog.show
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.e.Adapter.ShopAdapter
import com.example.e.Adapter.ShopHistoryAdapter
import com.example.e.Bean.Shop
import com.example.e.HttpUse.HttpUtil
import com.example.e.HttpUse.IP
import com.example.e.data.ShopSearchHistory
import kotlinx.android.synthetic.main.activity_search.*
import kotlinx.android.synthetic.main.activity_search.tvSearch
import kotlinx.android.synthetic.main.activity_search.txtName
import kotlinx.android.synthetic.main.activity_search_result.*
import kotlinx.android.synthetic.main.elem_history_layout.*
import kotlinx.android.synthetic.main.fragment_home.*
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import org.json.JSONArray
import java.io.*
import java.lang.StringBuilder

class SearchActivity : AppCompatActivity() {

    private val history_shop_data = ArrayList<ShopSearchHistory>()
    private val file_name1 = "shop_history"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search)

        load()
        show()
        refreshHistory()

        //val layoutManager = StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)
        //layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        //recyclerviewShopHistory.layoutManager = layoutManager
        recyclerviewShopHistory.layoutManager = LinearLayoutManager(this,RecyclerView.HORIZONTAL,true)


        tvSearch.setOnClickListener(){
            val intent = Intent(this,SearchResultActivity::class.java)

            val name = txtName.text.toString()
            intent.putExtra("name",name)
            startActivity(intent)

            if (name.isEmpty())
                Toast.makeText(this, "请输入", Toast.LENGTH_LONG).show()
            else{
                Toast.makeText(this, "${name}", Toast.LENGTH_LONG).show()
                history_shop_data.remove(ShopSearchHistory(name))
                history_shop_data.add(0, ShopSearchHistory(name))
                save()
                show()
                refreshHistory()
            }
        }
        ivClear.setOnClickListener(){
            history_shop_data.clear()
            refreshHistory()
            save()
        }
        iv_arrow1.setOnClickListener(){
            val intent = Intent(this,MainActivity::class.java)
            startActivity(intent)
        }
    }

     fun load(){
        val builder = StringBuilder()
        try {
            val input = openFileInput(file_name1)
            val reader = BufferedReader(InputStreamReader(input))
            reader.use {
                reader.forEachLine {
                    val info = parseHistory(it)
                    history_shop_data.add(info)
                }
            }
        }catch (e: IOException){
            e.printStackTrace()
        }
    }

    fun parseHistory(history_str:String):ShopSearchHistory{
        val name = history_str.trim()
        return ShopSearchHistory(name)
    }
    fun refreshHistory(){
        val adapter = ShopHistoryAdapter(history_shop_data)
        recyclerviewShopHistory.adapter = adapter

    }
    fun save(){
        try {
            val output = openFileOutput(file_name1, Context.MODE_PRIVATE)
            val writer = BufferedWriter(OutputStreamWriter(output))
            writer.use {
                for(item in history_shop_data){
                    writer.write("${item.name}")
                    writer.newLine()
                }
            }
        }
        catch (e: IOException) {
            e.printStackTrace()
        }
    }
    fun show(){
        if(history_shop_data.size > 10){
            history_shop_data.remove(history_shop_data.last())
        }
    }


}